package com.itheima.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dto.SetmealDto;
import com.itheima.entity.*;
import com.itheima.mapper.CategoryMapper;
import com.itheima.mapper.DishMapper;
import com.itheima.mapper.SetmealDishMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired(required = false)
    private SetmealMapper setmealMapper;
    @Autowired(required = false)
    private SetmealDishMapper setmealDishMapper;
    @Autowired(required = false)
    private CategoryMapper categoryMapper;
    @Autowired(required = false)
    private DishMapper dishMapper;

    @Override
    public void save(SetmealDto setmealDto) {
            setmealDto.setCreateTime(LocalDateTime.now());
            setmealDto.setUpdateTime(LocalDateTime.now());
            //先保存setmea的信息
            setmealMapper.saveWithSetmeal(setmealDto);
            //得到套餐的菜品，并补全信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setSort(0);
        }
        setmealDishMapper.saveWithSetmealDish(setmealDishes);


    }

    @Override
    public Page<SetmealDto> page(int page, int pageSize, String name) {
        PageHelper.startPage(page,pageSize);
        //得到套餐集合
      List<Setmeal> setmeals=  setmealMapper.findByName(name);
      //放进pageinfo
        PageInfo<Setmeal> setmealPageInfo=new PageInfo<>(setmeals);
        //创建一个空集合
        List<SetmealDto> setmealDtoList=new ArrayList<>();

        for (Setmeal setmeal : setmealPageInfo.getList()) {
            SetmealDto setmealDto = new SetmealDto();
            //拷贝setmaels去setmealDto
            BeanUtils.copyProperties(setmeal,setmealDto);
            //找到category——id对应的类别
            Category byId = categoryMapper.findById(setmeal.getCategoryId());
            setmealDto.setCategoryName(byId.getName());
            setmealDtoList.add(setmealDto);

        }

        Page<SetmealDto> page1=new Page<>(setmealDtoList,setmealPageInfo.getTotal(),page,pageSize,setmealPageInfo.getPages());
        return page1;

    }

    @Override
    @Transactional
    public void delete(List<Long> ids) {
       long count= setmealMapper.findstsatsCount(ids);
       if (count>0){
           throw  new ClassCastException("这个套餐已启用，不能删除");
       }
       setmealMapper.delete(ids);
       setmealDishMapper.deleteBySetmealIds(ids);

    }

    @Override
    public List<Setmeal> list(long categoryId, Integer status) {
      return   setmealMapper.list(categoryId,status);
    }

    /**
     * 修改套餐状态
     * @param status
     * @param ids
     */
    @Override
    public void status(Integer status, List<Long> ids) {
        //找出套餐对应的菜品id
            List<Long> dishList= setmealDishMapper.findBySetealId(ids);
        //得到菜品的状态
            List<Integer> statuslist = dishMapper.findByDishStatus(dishList);
        //判断菜品是否停售
        for (Integer integer : statuslist) {
            if (integer==0){
                setmealMapper.statusStop(ids);
            }
        }
        //如果存在停售 则把对应的套餐该未停售

        setmealMapper.status(status,ids);
    }

    /**
     * 响应套餐修改内容
     * @param id
     * @return
     */
    @Override
    public SetmealDto findById(long id) {
       Setmeal setmea= setmealMapper.findById(id);
       List<SetmealDish> setmealDishList= setmealDishMapper.findById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmea,setmealDto);
        setmealDto.setSetmealDishes(setmealDishList);
        return setmealDto;
    }

    /**
     * 修改套餐
     * @param setmealDto
     */
    @Override
    public void updateWithCategory(SetmealDto setmealDto) {
        setmealDto.setUpdateTime(LocalDateTime.now());

        setmealMapper.updateById(setmealDto);
       //先把菜品删掉再重新添加
        setmealDishMapper.deleteBySetmealId(setmealDto.getId());
        //重新添加菜品
        List<SetmealDish> setmealDishList=setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishList) {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setSort(0);
            setmealDish.setUpdateTime(setmealDto.getUpdateTime());
            setmealDish.setCreateTime(setmealDto.getCreateTime());
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
            setmealDish.setCreateUser(setmealDto.getCreateUser());


        }

        setmealDishMapper.saveWithSetmealDish(setmealDishList);
    }
}
